home *** CD-ROM | disk | FTP | other *** search
- Path: user2.mnsinc.com!huang
- From: huang@mnsinc.com (Szu-Wen Huang)
- Newsgroups: alt.msdos.programmer,comp.lang.c,comp.lang.pascal.misc
- Subject: Re: typecasting preferences
- Date: 18 Apr 1996 12:56:01 GMT
- Organization: Monumental Network Systems
- Distribution: world
- Message-ID: <4l5e51$1s4@news1.mnsinc.com>
- References: <4l020g$i9j@srvr1.engin.umich.edu> <Pine.A32.3.91.960416145209.106109C-100000@black.weeg.uiowa.edu> <4l4ldb$nkl@srvr1.engin.umich.edu>
- NNTP-Posting-Host: user.mnsinc.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- HASDI RODZMANN HASHIM (hasdi@news-server.engin.umich.edu) wrote:
- [snip]
- : The way I understand it, type-conversion is to transform the bits of one
- : variable so that it is representable as the type to be converted into. eg.
- : (int)my_float. type-casting doesn't transform the bits at all, but force
- : the compiler to treat that variable as the specified type... which is
- : highly unportable. Why would anyone want to typecast then? Can somebody
- : clarify this for me?
-
- Sure. You typecast when you know better than the compiler. For example,
- without typecasting, malloc() would need to have a version for char *,
- int *, and whatnot, and allocating memory for a user-defined structure
- would not be possible. As it is, it returns a void * which is explicitly
- or implicitly cast to the correct type. A more dangerous (but nonetheless
- oft-seen) use is to enable you to code assumptions on data. For instance,
- if you have 4 chars that you know to be actually int, like so:
-
- char really_an_int[4];
-
- you can:
-
- int x = *(int *)really_an_int;
-
- Mind you, this is stinking code, but it does get used.
-